﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH.Admin
{
    public partial class Nhaxuatban : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //hienTenNXB();

            }
            hienNXB();
            /*int dem= grvnhaxuatban.Rows.Count;
            string sdem=dem.ToString();
            lblNXB.Text = sdem;*/
        }
        private void hienNXB()
        {
            using (DataTable tbl = getNXB())
            {
                grvnhaxuatban.DataSource = tbl;
                grvnhaxuatban.DataBind();
            }
        }
        private DataTable getNXB()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlSelect = "SELECT MaNXB, TenNXB, SDT, Fax, Diachi"
                + " FROM NHAXUATBAN"
                + " ORDER BY TenNXB ASC";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }
        protected void grvNhaxuatban_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int nhaxuatbanID = Convert.ToInt32(e.CommandArgument);
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("nhaxuatban_Xoa", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("MaNXB", nhaxuatbanID);
                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hienNXB();
                    }
                }
            }
        }



        protected bool NXB_Check()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlCheck = "SELECT MaNXB, TenNXB FROM NHAXUATBAN WHERE TenNXB like N'" + txtNXB.Text + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlCheck, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            lblNXB.Text = "Tên nhà xuất bản đã tồn tại";
                            return true;
                        }
                        else
                        {
                            lblNXB.Text = String.Empty;
                            return false;
                        }
                    }
                }
            }

        }
        protected void ThemmoiNXB()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Nhaxuatban_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@TenNXB", txtNXB.Text);
                    Cmd.Parameters.AddWithValue("@SDT", txtSDT.Text);
                    Cmd.Parameters.AddWithValue("@Fax", txtFax.Text);
                    Cmd.Parameters.AddWithValue("@Diachi", txtDiachi.Text);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        
        protected void btnThem_Click(object sender, EventArgs e)
        {
            if (NXB_Check() == false)
                ThemmoiNXB();
      
                
            hienNXB();
            // Boqua();
        }
        protected void Boqua()
        {
            txtNXB.Text = txtSDT.Text = txtFax.Text = txtDiachi.Text = String.Empty;
            txtNXB.Focus();
        }
        protected void btnBoqua_Click(object sender, EventArgs e)
        {
            Boqua();
        }
        //////////////////////
        /* private void hienTenNXB()
         {
             string sqlTenNXB_Select = "SELECT MaNXB,TenNXB FROM NHAXUATBAN";
             string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
             using (SqlConnection Cnn = new SqlConnection(connectionString))
             {
                 using (SqlCommand Cmd = new SqlCommand(sqlTenNXB_Select, Cnn))
                 {
                     Cmd.CommandType = CommandType.Text;
                     Cnn.Open();
                     using (SqlDataReader rd = Cmd.ExecuteReader())
                     {
                         ddlNXB.Items.Insert(0, "-- Chọn nhà xuất bản --");
                         if(rd.HasRows)
                             while (rd.Read())
                             {
                                 ddlNXB.Items.Add(new ListItem(rd["TenNXB"].ToString(),
                                                    rd["MaNXB"].ToString()));
                             }
                         rd.Close();
                     }
                     Cnn.Close();
                 }
             }
         }
         private DataTable layNXBtheoMa(int MaNXB)
         { 
             string sqlSelect=string.Format("SELECT MaNXB,TenNXB,SDT,Fax,Diachi FROM NHAXUATBAN WHERE MaNXB='"+MaNXB+"'");
             string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
             using (SqlConnection Cnn = new SqlConnection(connectionString))
             {
                 using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                 {
                     using (SqlDataAdapter da = new SqlDataAdapter())
                     {
                         da.SelectCommand = Cmd;
                         DataTable mytable = new DataTable("tblNXB");
                         da.Fill(mytable);
                         return mytable;
                     }
                 }
             }
         }
         protected void btnXem_Click(object sender, EventArgs e)
         {
             int catID = Convert.ToInt32(ddlNXB.SelectedValue);
             using (DataTable tblNXB = layNXBtheoMa(catID))
             {
                 DataView dvNXB = new DataView(tblNXB);
                 if (ViewState["SortExp"] != null)
                     dvNXB.Sort = ViewState["SortExp"].ToString();
                 grvnhaxuatban.DataSource = dvNXB;
                 grvnhaxuatban.DataBind();
             }
         }
         protected void grvNXB_Sorting(object sender, GridViewSortEventArgs e)
         {
             string sortExp = e.SortExpression;
             if (ViewState["SortExp"] != null)
                 if (ViewState["SortExp"].ToString().IndexOf(" ASC") > 0)
                     sortExp += " DESC";
                 else
                     sortExp += " ASC";
             else
                 sortExp += " ASC";
             ViewState["SortExp"] = sortExp;
             //btnXem_Click(sender, e);
             //sắp xếp
             DataView sortedView = new DataView(getNXB());
             sortedView.Sort = sortExp;
             grvnhaxuatban.DataSource = sortedView;
             grvnhaxuatban.DataBind();
             //end sắp xếp
         }*/

    }
}